package user

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/logic/withdraw"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/common"
	"gitee.com/liuxuezhan/ar-platform/base-common/consts"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserBalanceInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户页面--用户余额
func NewUserBalanceInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserBalanceInfoLogic {
	return &UserBalanceInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserBalanceInfoLogic) UserBalanceInfo(req *types.UserBalanceInfoRequest) (resp *types.UserBalanceInfoResponse, err error) {
	userId := headInfo.GetTokenUid(l.ctx)
	in := rpc.GetBalanceRequest{UserId: userId}
	balanceResp, err := l.svcCtx.AssetsRpc.User.GetBalance(l.ctx, &in)
	if err != nil {
		return nil, err
	}
	if withdraw.IsOpen(l.svcCtx, l.ctx, common.AssetsInOut, "status") == 1 {
		balanceResp.Balance = float64(int64(balanceResp.Balance))
		balanceResp.TotalRecharge = float64(int64(balanceResp.TotalRecharge))
		balanceResp.TotalWithdraw = float64(int64(balanceResp.TotalWithdraw))
	}
	resp = &types.UserBalanceInfoResponse{Balance: balanceResp.Balance}
	if withdraw.IsOpen(l.svcCtx, l.ctx, common.AssetsTotal, "status") == 1 {
		resp.TotalRecharge = balanceResp.TotalRecharge
		resp.TotalWithdraw = balanceResp.TotalWithdraw
		resp.ShowTatal = int(consts.IntBoolTrue)
	}
	return resp, nil
}
